package org.example.node;

import com.alibaba.fastjson2.JSON;

/**
 * @author liyishan
 * @date 2024/2/19 09:48
 * @apiNote
 */

public class LCR140TrainingPlan {

    public static void main(String[] args) {
        ListNode  head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = new ListNode(5);
        ListNode node = trainingPlan1(head, 2);
        System.out.println(JSON.toJSON(node));
    }

    public static ListNode trainingPlan(ListNode head, int cnt) {
        int n = 0;
        ListNode node;
        for (node = head; node != null; node = node.next) {
            n++;
        }
        for (node = head; n > cnt; n--) {
            node = node.next;
        }
        return node;
    }


    public static ListNode trainingPlan1(ListNode head, int cnt) {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && cnt > 0){
            fast = fast.next;
            cnt--;
        }
        while(fast != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }



}
